package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.LogDao;
import com.chunfeng.mypen.entity.Log;
import com.chunfeng.mypen.properties.SystemProperties;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 日志业务层实现
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/22
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogDao, Log> implements LogService {

    /**
     * 日志Dao
     */
    @Autowired
    private LogDao logDao;
    /**
     * 系统设置
     */
    @Autowired
    private SystemProperties systemProperties;

    /**
     * 条件查询日志
     *
     * @param current 当前页
     * @param size    每页长度
     * @param log     条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<Log>> lookAllLog(Integer current, Integer size, Log log) {
        Page<Log> logPage = logDao.selectAllLog(new Page<>(current, size), log);
        //拿到列表
        List<Log> logs = logPage.getRecords();
        if (logs.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        Map<String, Object> page = new HashMap<>();
        //记录分页总数
        page.put(systemProperties.getPageKeyName(), logPage.getTotal());
        return JsonRequest.success(logs, page);
    }

    /**
     * 新增一条日志信息
     *
     * @param log 日志信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> insertOneLog(Log log) {
        int column = logDao.insert(log);
        if (column < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 更新一条日志
     *
     * @param log 日志信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateOneLog(Log log) {
        Log log1 = logDao.selectById(log.getId());
        if (Objects.isNull(log1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = logDao.updateById(log);
        if (column < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除日志
     *
     * @param ids 日志ID
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteLog(List<String> ids) {
        Integer size = logDao.selectCountById(ids);
        if (size != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = logDao.deleteBatchIds(ids);
        if (column < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }
}
